﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace BattleshipGameClient
{
    [Serializable]
    public class Fleet
    {
        private Dictionary<Ship.Type, Ship> ships;
        public Dictionary<Ship.Type, Ship> Ships
        {
            get { return ships; }
            set { ships = value; }
        }

        public Fleet(bool isOpponent)
        {
            this.ships = new Dictionary<Ship.Type, Ship>();
            if (isOpponent)
            {
                ships.Add(Ship.Type.Battleship, new Ship(Ship.Type.Battleship)); 
            }
        }
        
        public Ship shoot(Point coord) {
            foreach (Ship ship in ships.Values)
            {
                if (ship.shoot(coord))
                    return ship;
            }
            return null;
        }

        public bool isSunk()
        {
            foreach (Ship ship in ships.Values)
            {
                if (!ship.IsSunk)
                {
                    return false;
                }
            }
            return true;
        }

        internal void hit(Point coord, Ship.Type hitBoatType)
        {
            Ship s = getShip(hitBoatType);
            if (s is Ship)
                s.addHit();
        }

        private Ship getShip(Ship.Type type)
        {
            foreach (Ship ship in ships.Values)
            {
                if (ship.getShipType() == type)
                {
                    return ship;
                }
            }
            return null;
        }
    }
}
